package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.mapper.OrderExceptionNoteMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxOrderExceptionNote;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class OrderExceptionNoteService {
    @Resource
    private OrderExceptionNoteMapper orderExceptionNoteMapper;

    public boolean insertByBean(YxOrderExceptionNote yxOrderExceptionNote) {
        yxOrderExceptionNote.setCreateTime(new Date());
        yxOrderExceptionNote.setCreateTime(new Date());
        return orderExceptionNoteMapper.insert(yxOrderExceptionNote) > 0 ? true : false;
    }

    public List<YxOrderExceptionNote> selectByOrderExceptionId(Integer id) {
        Example example = new Example(YxOrderExceptionNote.class);
        example.createCriteria().andEqualTo("orderExceptionId", id);
        example.orderBy("createTime").desc();
        List<YxOrderExceptionNote> result = orderExceptionNoteMapper.selectByExample(example);
        Pattern pattern = Pattern.compile("-?[0-9]+(\\\\.[0-9]+)?");
        result.stream().forEach(e->{
            String adminName = e.getAdminName();
            if(!StringUtils.isEmpty(adminName)){
                Matcher m = pattern.matcher(adminName);
                //非id值
                if(!m.matches()){
                    StringBuffer sb = new StringBuffer(adminName);
                    if(sb.length() > 1){
                        sb.replace(1,2,"*");
                    }
                    e.setAdminName(sb.toString());
                }
            }
        });
        return result;
    }
}
